import org.junit.Test;

import java.util.concurrent.*;

public class FutureAndCallableExample {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable <String> callable = () ->{
            System.out.println("Entered Callable");
            Thread.sleep(200);
            return "Hello from Callable";
        };
        FutureTask<String> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println("Do something else while callable is getting executed");
        System.out.println("Retrieved: " + futureTask.get());
    }


    @Test
    public void t2() throws ExecutionException, InterruptedException {
        Callable <String> callable = () ->{
            System.out.println("Entered Callable");
            Thread.sleep(200);
            return "Hello from Callable";
        };
        ExecutorService executor = Executors.newSingleThreadExecutor();
        System.out.println("Submitting Callable");
        Future<String> future = executor.submit(callable);
        System.out.println("Do something else while callable is getting executed");
        System.out.println("Retrieved: " + future.get());
    }


    @Test
    public void t3(){
        ThreadLocal<A> threadLocal = new ThreadLocal<>();
        A a = new A("1","liwenfei");
        threadLocal.set(a);
        A a1 = threadLocal.get();
        System.out.println(a1);
    }


}
